package com.elephant.core.utils;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cdn.model.v20141111.RefreshObjectCachesRequest;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Created by admin on 2018/6/14.
 */

public class AliyunCDNUtil {

    public static DefaultAcsClient client;
    public static void main(String[] args) throws ClientException {
        // TODO Auto-generated method stub
        init();
        String[] urls = {
             "http://cdn.tiku.zhan.com/TpoXiaoZhan.apk",
        };
        for (String path : urls) {
            PurgeObjectCaches(path);
        }
    }

    public static void init() throws ClientException {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "J0JKBCKJbtiyT3Ot", "B4jPUOyDpONWpCJhFmUwRUvtGXFDLb");
        client = new DefaultAcsClient(profile);
    }

    public static void PurgeObjectCaches(String path) {
        RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
        request.setObjectPath(path);
        //刷新类型，默认是File
        request.setObjectType("File");

        try {
            HttpResponse httpResponse = client.doAction(request);
            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getHttpContent()));
            //todo something.
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
    }
}